package com.lishuang.org.controller;

import com.lishuang.basic.annotation.PreAuthorize;
import com.lishuang.basic.util.AjaxResult;
import com.lishuang.basic.util.PageList;
import com.lishuang.org.domain.Department;
import com.lishuang.org.query.DepartmentQuery;
import com.lishuang.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    //@PreAuthorize(name = "查询单个部门",sn = "department:findOne") //value后面用来做权限校验
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    //@PreAuthorize(name = "查询所有部门",sn = "department:findAll") //value后面用来做权限校验
    public List<Department> findAll() {
        return departmentService.loadAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    //@PreAuthorize(name = "分页查询部门",sn = "department:queryPage") //value后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryPage(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除数据")
    @PreAuthorize(name = "删除单个部门",sn = "department:delete") //value后面用来做权限校验
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }

    }

    @PatchMapping
    @ApiOperation(value = "通过ids批量删除数据")
    @PreAuthorize(name = "批量删除部门",sn = "department:batchDelete") //value后面用来做权限校验
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }

    }

    @PutMapping
    @ApiOperation(value = "添加或者修改(通过有无ID区分)")
    //@PreAuthorize(name = "添加或者修改部门",sn = "department:addOrUpdate") //value后面用来做权限校验
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树")
    //@PreAuthorize(name = "查询部门树",sn = "department:deptTree") //value后面用来做权限校验
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}
